package com.foodmarket.controller;
import com.foodmarket.pojo.Buyers;
import com.foodmarket.pojo.Result;
import com.foodmarket.service.UserService;
import com.foodmarket.utils.JwtUtil;
import com.foodmarket.utils.MD5Util;
import com.foodmarket.utils.ThreadLocalUtil;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/buyer")
public class BuyerController {

    @Autowired
    private UserService userService;

    @PostMapping("/register") // 注册买家
    public Result registerBuyer( String username, String password, String phone, String email,String name) {

        if(username.equals("")||password.equals("")||phone.equals("")||email.equals("")||name.equals(""))
        {
            return Result.error("请输入完整的数据");
        }

        // 查询用户
        Buyers buyer = userService.findByBuyerName(username);
        if (buyer == null) {
            // 用户不存在，进行注册
            userService.registerBuyer(username, password,phone,email,name);
            return Result.success("买家注册成功");
        } else {
            // 用户名已存在
            return Result.error("用户名已或手机号被占用");
        }
    }
    @PostMapping("/login")//登录判断
    public Result<String> buyerLogin(String username, String password){
        if(username.equals("")||password.equals(""))
        {
            return Result.error("请输入完整的信息");
        }
        //根据用户名查询用户
        Buyers loginBuyer = userService.findByBuyerName(username);
        //判断用户名存在
        if (loginBuyer==null){
            return Result.error("用户名不存在");
        }
        //判断密码是否正确
        if (MD5Util.getMD5String(password).equals(loginBuyer.getPassword())){

            Map<String,Object> claims=new HashMap<>();
            claims.put("id",loginBuyer.getBuyerId());
            claims.put("username",loginBuyer.getUsername());
            String token = JwtUtil.genToken(claims);
            //密码正确
            return Result.success(token);
        }
        return Result.error("密码错误");
    }
    @GetMapping("/buyerInfo")
    public Result<Buyers> userInfo(){
        //根据用户名查询用户
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        Buyers buyer=userService.findByBuyerName(username);
        return Result.success(buyer);
    }

    @PutMapping("update")//更新
    public Result update(@RequestBody Buyers buyers)
    {
        userService.updateBuyer(buyers);
        return Result.success();
    }

    @PatchMapping("updateBuyerPwd")//修改密码
    public Result updatePwd(@RequestBody Map<String,String> params){
        //校验参数
        String oldBuyerPwd=params.get("oldBuyer_pwd");
        String newBuyerPwd=params.get("newBuyer_pwd");
        String reBuyerPwd=params.get("reBuyer_pwd");

        if (!StringUtils.hasLength(oldBuyerPwd)) {
            return Result.error("缺少旧密码参数");
        }
        if (!StringUtils.hasLength(newBuyerPwd)) {
            return Result.error("缺少新密码参数");
        }
        if (!StringUtils.hasLength(reBuyerPwd)) {
            return Result.error("缺少确认密码参数");
        }
        //原密码是否正确
        Map<String,Object> mapBuyer = ThreadLocalUtil.get();
        String username= (String) mapBuyer.get("username");
        Buyers loginBuyer=userService.findByBuyerName(username);

        if (! loginBuyer.getPassword().equals(MD5Util.getMD5String(oldBuyerPwd))){
            return Result.error("原密码填写不正确");
        }
        if (!reBuyerPwd.equals(newBuyerPwd)){
            return Result.error("两次填写的密码不一样");
        }
        //更新
        userService.updateBuyerPwd(newBuyerPwd);
        return Result.success();
    }

}